<style lang="scss" scoped>
  @import '../../styles/common/loading.less';
  .container{
    border: none !important;
    margin-top: 4px;
  }
  .el-col-offset-1 {
    margin-left: 0%;
  }
  .button_a {
    background-color: rgba(255, 138, 0, 1);
    border-color: rgba(255, 138, 0, 1);

  }
  .col_a {
    margin-left: 2%;
  }
  .el-card {
    height: 200px;
  }

  .el-button--primary {
    background-color: rgba(255, 138, 0, 1);
    border-color: rgba(255, 138, 0, 1);
  }
</style>
<template>
    <div>
        <div class="container">
            <div>
                <el-form :model="formInline" class="select-submit-form">
                    <el-row type="flex">
                        <el-col :span="8" >
                            <el-form-item label="任务类型">
                                <el-select v-model="formInline.taskType" style="width: 100%">
                                    <!-- <el-option label="案件登记" value="case_main"></el-option>
                                    <el-option label="核赔" value="undwrt"></el-option>
                                    <el-option label="注销审核" value="cancel_audit"></el-option>
                                    <el-option label="调查处理" value="survey"></el-option>
                                    <el-option label="调查审核" value="survey_audit"></el-option>
                                    <el-option label="结算审核" value="settle_audit"></el-option>
                                    <el-option label="重开处理" value="recase"></el-option>
                                    <el-option label="重开审核" value="recase_audit"></el-option> -->
                                    <el-option v-for="item in taskTypeList" :key="item.taskType" :label="item.taskName" :value="item.taskType"> </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="业务号">
                                <el-input v-model="formInline.caseNo" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="处理人">
                                <el-input v-model="formInline.handlerCode" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <div class="select-btn-group">
                                <el-button type="primary" size="medium"  @click="handleSearch">查询</el-button>
                                <el-button type="primary" size="medium" @click="handleReset">重置</el-button>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>

                <el-table
                    :data="tableData"
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    style="margin: 30px 0;border: 1px solid #d6d9d8;"
                    v-loading="loading"
                    element-loading-text="Loading....."
                >
                    <el-table-column prop="caseNo" label="业务号" align="center"></el-table-column>
                    <el-table-column prop="taskType" label="环节" :formatter="formatTaskType" align="center"></el-table-column>
                    <el-table-column prop="handlerName" label="当前处理人姓名" align="center"></el-table-column>
                    <el-table-column prop="handlerCode" label="当前处理人工号" align="center"></el-table-column>
                    <el-table-column prop="taskState" label="状态" :formatter="formatTaskState" align="center"></el-table-column>
                    <el-table-column prop="flowInTime" label="流入时间" align="center"></el-table-column>
                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="taskReassignClick(scope.row)"
                            >任务改派</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <div class="pagination">
                    <div v-show=" pagination.totalPage>0" class="index_page">
                        <el-pagination :current-page="pagination.currentPage" :page-size="pagination.rowsNumber"
                                        :page-sizes="[10, 25, 50, 100]"
                                        :total="pagination.totalPage" background layout="prev, pager, next,sizes,total,jumper"
                                        @size-change="handleSizeChange" @current-change="queryFun">
                        </el-pagination>
                    </div>
                </div>

                <!--弹框-->
                <el-dialog
                    title="任务改派"
                    :visible.sync="dialogFormVisible"
                    width="75%"
                    center>
                    <el-card class="box-card" width="100%">
                        <div slot="header" class="clearfix">
                            <span>信息列表</span>
                        </div>
                        <el-table
                            :data="taskTable"
                            border
                            class="table"
                            ref="multipleTable"
                            header-cell-class-name="table-header"
                        >
                            <el-table-column prop="caseNo" label="业务号" align="center"></el-table-column>
                            <el-table-column prop="taskType" label="环节" :formatter="formatTaskType" align="center"></el-table-column>
                            <el-table-column prop="handleComrCode" label="处理人机构代码" align="center"></el-table-column>
                            <el-table-column prop="handleComrName" label="处理人机构名称" align="center"></el-table-column>
                            <el-table-column prop="handlerCode" label="原处理人工号" align="center"></el-table-column>
                            <el-table-column prop="handlerName" label="原处理人姓名" align="center"></el-table-column>
                        </el-table>
                    </el-card>
                    <el-card class="box-card" width="100%">
                        <div slot="header" class="clearfix">
                            <span>任务改派</span>
                        </div>
                        <el-form :model="taskReassign" class="select-submit-form">
                            <el-row type="flex">
                                <el-col :span="8" >
                                    <el-form-item label="改派原因">
                                        <el-select v-model="taskReassign.distReason" @change="changeDistReason" placeholder="全部">
                                            <el-option
                                            v-for="item in distReasonList"
                                            :key="item.codeCode"
                                            :label="item.codeCName"
                                            :value="item.codeCode">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8" >
                                    <el-form-item label="处理人机构">
                                        <el-select v-model="taskReassign.handleComrCode"  @change="changeHandleComrCode">
                                             <el-option v-for="item in handleComList" :key="item.comCode" :label="item.comCName" :value="item.comCode"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8" >
                                    <el-form-item label="处理人">
                                        <el-select v-model="taskReassign.handlerCode"  @change="changeHandlerCode">
                                            <el-option v-for="item in handlerList" :key="item.handlerCode" :label="item.handlerValue" :value="item.handlerCode"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </el-card>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="onSubmit">确  认</el-button>
                        <el-button type="info" @click="dialogFormVisible = false">取  消</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</template>

<script>
import{ queryReassignTask, queryCodeList, updateReassignTask, queryAllLowerCompanysList, queryGradeTaskUserList }from '@/api/baseDate';
import Cookies from 'js-cookie';
export default {
    name: 'taskReassignment',
    data() {
        return {
            loading: false,
            pagination: {
                currentPage: 1, // 当前页
                rowsNumber: 10, // 每页条数
                totalPage: 0 // 总条数
            },
            taskTypeList:[
                {
                    taskType:'case_main',
                    taskName:'案件登记'
                },
                {
                    taskType:'undwrt',
                    taskName:'核赔'
                },
				{
                    taskType:'cancel_audit',
                    taskName:'注销审核'
                },
                {
                    taskType:'survey',
                    taskName:'调查处理'
                },
				{
                    taskType:'survey_audit',
                    taskName:'调查审核'
                },
                {
                    taskType:'settle_audit',
                    taskName:'结算审核'
                },
                {
                    taskType:'recase_audit',
                    taskName:'重开审核'
                },
                {
                    taskType:'refund_ticket',
                    taskName:'退票处理'
                },
                {
                    taskType:'refund_ticket_audit',
                    taskName:'退票审核'
                }
            ],
            formInline:{
                taskType:'',
            },
            dialogFormVisible: false,
            tableData: [],
            taskTable:[],
            distReasonList:[],
            taskReassign:{
                distReason:'',
                handleComrCode:'',
                handlerCode:'',
            },
            handleComList:[],
            handlerList:[],
            codeCode:'',
            codeName:'',
            handleComrCode:'',
            handleComrName:'',
            handlerCode:'',
            handlerName:'',
            taskTypes: '',
        }
    },
    created() {
        // 任务类型添加默认值
        this.formInline.taskType = this.taskTypeList[0].taskType;
    },
    methods: {
        handleSizeChange(pageSize){ // 改变当前页条数事件
            this.pagination.rowsNumber=pageSize
            this.queryReassignTaskList();
        },
        queryFun(pageNum){ // 改变当前页事件
            this.pagination.currentPage = pageNum;
            this.queryReassignTaskList();
        },
        // 查询
        handleSearch(){
            this.queryReassignTaskList();
        },
        // 重置
        handleReset(){

        },
        queryReassignTaskList(){
            let param = {
                ...this.formInline,
                pageNum: this.pagination.currentPage,
                pageSize: this.pagination.rowsNumber,
                comCode:Cookies.get("makeCom")
            }
            // 禁止重复点击
            if(this.loading){
                return;
            }
            this.loading = true;
            queryReassignTask(param).then((res)=> this.handleTaskListResponse(res));
        },
        handleTaskListResponse(res){
            this.loading = false;
            if(res){
                if(res.data.resultCode){
                    if(res.data.resultCode != '0000'){ 
                        // 返回失败
                        this.$message.error(res.data.resultMsg);
                    }else{
                        // 返回成功
                        this.tableData = res.data.list;
                        this.pagination.totalPage = res.data.total;
                    }
                }
            }else{
                this.$message.error('系统异常'); 
            }
        },
        // 改派原因
        queryDistReasonList(){ 
            const param = {
                codeType:'DistReason'
            }
            queryCodeList(param).then(res=> {
                this.distReasonList = res.data
            })
        },
        // 打开任务改派弹框
        taskReassignClick(row){
            this.dialogFormVisible = true;
            this.taskReassign.distReason = '';
            this.taskReassign.handleComrCode = '';
            this.taskReassign.handlerCode = '';
            this.codeCode = '';
            this.codeName = '';
            this.handleComrCode = '';
            this.handleComrName = '';
            this.handlerCode = '';
            this.handlerName = '';
            this.taskTypes = row.taskType;
            this.queryDistReasonList();
            this.queryHandleComList();
            this.taskTable = [];
            this.taskTable.push(row);
        },
        // 查询所有机构机构
        queryHandleComList(){
            let param = {
                // comCode: '',
                // upperComCode: '',
                // comCName: '',
                // addressCName:'',
                comCode: Cookies.get("makeCom"),
            }
            queryAllLowerCompanysList(param).then(res=> {
                this.handleComList = res.data;
            })

        },
        // 根据机构和岗位查询处理人
        queryHandleList(taskType,comCode){
            let param = {
                taskType: taskType,
                comCode: comCode,
                riskCode: ''
            }
            queryGradeTaskUserList(param).then(res=> {
                //this.handlerList = res.data;
                this.handlerList=res.data.map(item=>{
                    return {
                            handlerCode: item.userCode,
                            handlerName: item.userName,
                            handlerValue: item.userName +" - " +item.userCode
                        }
                    })
            })

        },
        // 监听改派原因
        changeDistReason(val){
            this.distReasonList.map((item)=>{
                if(item.codeCode === val){
                    this.codeCode = item.codeCode;
                    this.codeName = item.codeCName;
                }
            })
        },
        // 监听机构
        changeHandleComrCode(val){
            this.handleComList.map((item)=>{
                if(item.comCode === val){
                    this.handleComrCode = item.comCode;
                    this.handleComrName = item.comCName;
                    this.queryHandleList(this.taskTypes, this.handleComrCode);
                }
            })
        },
        // 监听处理人
        changeHandlerCode(val){
            this.handlerList.map((item)=>{
                if(item.handlerCode === val){
                    this.handlerCode = item.handlerCode;
                    this.handlerName = item.handlerName;
                }
            })
        },
        onSubmit() {
            if (this.codeCode == undefined || this.codeCode == null || this.codeCode == '' ){
                this.$message.warning(`请选择改派原因！`);
                return;
            }
            let params = {
                id:this.taskTable[0].id,
                caseNo:this.taskTable[0].caseNo,
                oldHandlerCode:this.taskTable[0].handlerCode,
                oldHandlerName:this.taskTable[0].handlerName,
                codeCode:this.codeCode,
                codeName:this.codeName,
                handleComrCode:this.handleComrCode,
                handleComrName:this.handleComrName,
                handlerCode:this.handlerCode,
                handlerName:this.handlerName,
            }
            // 禁止重复点击
            if(this.loading){
                return;
            }
            this.loading = true;
            updateReassignTask(params).then(res=> {
                this.loading = false;
                if(res){
                    if(res.data.resultCode){
                        if(res.data.resultCode != '0000'){ 
                            // 返回失败
                            this.$message.error(res.data.resultMsg);
                        }else{
                            this.$message.success(res.data.resultMsg);
                            this.queryReassignTaskList();
                        }
                    }else{
                        this.$message.error('系统异常'); 
                    }
                }else{
                    this.$message.error('改派失败'); 
                }
            })
            this.dialogFormVisible = false;
        },
    }
}
</script>
